class Solution647 {
    public int countSubstrings(String s) {
        int n = s.length() ;
        char[] arr = s.toCharArray() ;
        int sum = 0 ;
        boolean[][] dp = new boolean[n][n] ;
        for(int i=n-1 ; i >= 0 ; i --){
            for(int j=i ; j < n ; j ++){
                if(arr[i] == arr[j]){
                    if(i == j){
                        dp[i][j] = true ;
                    }else if(i == j-1){
                        dp[i][j] = true ;
                    }else{
                        dp[i][j] = dp[i+1][j-1] ;
                    }
                }
                if(dp[i][j]){
                    sum ++ ;
                }
            }
        }

        return sum ;
    }
}